//
//  HMStatusOriginalView.swift
//  iOSWeibo11
//
//  Created by Apple on 16/7/22.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

/// 原创View,显示原创的信息
class HMStatusOriginalView: UIView {
    
    /// 微博模型
    var status: HMStatus? {
        didSet {
            // 当cell给原创view设置模型,根据模型来显示内容
            // 头像
            if let profile_image_url = status?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)
//                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
                
                HMAsyncDrawImage.sharedAsyncDrawImage.hm_AsyncDrawImage(url!.absoluteString, drawFinish: { (image) -> () in
                    self.iconView.image = image
                })
            }
            
            // 名称
            nameLabel.text = status?.user?.screen_name
            
            // 时间, cell要显示的时候,控制器会设置模型,可以拿到控制器设置的模型
            timeLabel.text = NSDate.date(status?.created_at ?? "未知时间")?.sinaDateDescription()
            
            // 来源
            sourceLabel.text = status?.source
            
            // 模型提供的数据,但是View用起来不怎么好用,用该让模型提供一个好用数数据
            // 会员等级
            memberImageView.image = status?.user?.mbrankImage
            
            // 认证类型
            verifiedImageView.image = status?.user?.verifiedImage
            
            // 微博内容
            contentLabel.text = status?.text
            
            // 将原创view的图片设置给配图视图,里面会自己更新size约束
            pictureView.imageURLs = status?.storePicutureURLs
            
            // 当没有配图的时候 pictureView size = 0, 配图顶部的到contentLabel底部的间距 = 0
            pictureView.snp_updateConstraints { (make) -> Void in
                // 有图片间距为8,没有图片间距为0
                var margin: CGFloat = 0
                if status?.storePicutureURLs != nil && status?.storePicutureURLs?.count > 0 {
                    margin = CellMargin
                }
                
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    /// 设置UI
    private func setupUI() {
        // 测试阴影
        memberImageView.layer.shadowColor = UIColor.greenColor().CGColor
        memberImageView.layer.shadowOffset = CGSize(width: 1, height: 1)
        memberImageView.layer.shadowRadius = 1
        memberImageView.layer.shadowOpacity = 0.8
        
        // iOS阴影不会离屏渲染
        memberImageView.layer.shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 15, height: 15)).CGPath
        
//        iconView.layer.cornerRadius = 17.5
//        iconView.layer.masksToBounds = true
        
        // 1.添加子控件
        self.addSubview(iconView)
        self.addSubview(nameLabel)
        self.addSubview(timeLabel)
        self.addSubview(sourceLabel)
        self.addSubview(memberImageView)
        self.addSubview(verifiedImageView)
        self.addSubview(contentLabel)
        self.addSubview(pictureView)

        // 2.添加约束
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(CellMargin)
            make.size.equalTo(CGSize(width: CellIconWH, height: CellIconWH))
        }
        
        // 名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CellMargin)
            make.top.equalTo(iconView)
        }
        
        // 时间label
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        // 来源label
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        // 会员等级
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        // 认证图标
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        // 微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(CellMargin)
        }
        
        // 配图视图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            
            // 测试大小,其实应该根据图片的数量计算
            make.size.equalTo(CGSize(width: 359, height: 359))
        }
        
        // 约束原创view的底部和 微博文本配图底部重合
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(CellMargin)
        }
    }

    // MARK: - 懒加载
    /// 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    
    /// 时间label
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor())
    
    /// 来源label
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor())
    
    /// 会员等级
    private lazy var memberImageView: UIImageView = UIImageView(frame: CGRectZero)
    
    /// 认证图标
    private lazy var verifiedImageView: UIImageView = UIImageView(frame: CGRectZero)
    
    /// 微博文本内容
    private lazy var contentLabel: FFLabel = {
        let label = FFLabel(textColor: UIColor.darkGrayColor(), fontSize: CellFontSize)
        
        // 设置多行
        label.numberOfLines = 0
        
        label.labelDelegate = self
        
        // 文字换行宽度
        label.preferredMaxLayoutWidth = UIScreen.width() - 2 * CellMargin
        
        return label
    }()
    
    /// 配图视图
    private lazy var pictureView: HMStatusPictureView = HMStatusPictureView()
}

extension HMStatusOriginalView: FFLabelDelegate {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        print("点中了: \(text)")
    }
}
